﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PcProject.Application.Service.Order;
using PcProject.Domain.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MiniPc.API.Controllers
{
    /// <summary>
    /// 订单控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize] //授权过滤
    public class OrderController : ControllerBase
    {
        /// <summary>
        /// 构造函数依赖注入
        /// </summary>
        private IOrderService _orderService;
        /// <summary>
        /// 函数依赖注入
        /// </summary>
        /// <param name="orderService"></param>
        public OrderController(IOrderService orderService)
        {
            _orderService = orderService;
        }


        /// <summary>
        /// 订单显示查询分页
        /// </summary>
        /// <param name="number">查询参数</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageSize">条数</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index(string number = "", int pageIndex = 1, int pageSize = 3)
        {
            try
            {
                int totalCount;
                number += "";
                List<OrderAll> lst = _orderService.GetOrdersAsync(number, pageIndex, pageSize, out totalCount);
                return Ok(new { data = lst, code = 0, msg = "", count = totalCount });
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddOrder([FromForm] Orders obj)
        {
            try
            {
                int n = await _orderService.InsertAsync(obj);
                if (n > 0)
                {
                    return Ok(new { data = obj, code = 1 });
                }
                else
                {
                    return Ok(new { data = n, code = 0 });
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DelOrder(int id)
        {
            try
            {
                int n = await _orderService.DeleAsync(id);
                if (n > 0)
                {
                    return Ok(new { data = n, code = 1 });
                }
                else
                {
                    return Ok(new { data = n, code = 0 });
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 反填订单
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Fill(int id)
        {
            try
            {
                OrderAll n = await _orderService.FillAsync(id);
                return Ok(new { data = n, code = 1 });
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 修改订单
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UptOrder([FromForm] Orders obj)
        {
            try
            {
                int n = await _orderService.UptAsync(obj);
                if (n > 0)
                {
                    return Ok(new { data = n, code = 1 });
                }
                else
                {
                    return Ok(new { data = n, code = 0 });
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

    }
}
